#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct TreeNode {
    char val;
    struct TreeNode* left;
    struct TreeNode* right;
};

// 根据前序和中序遍历结果构造二叉树
struct TreeNode* buildTree(char* preorder, char* inorder, int preStart, int preEnd, int inStart, int inEnd) {
    if (preStart > preEnd || inStart > inEnd) return NULL;
    char rootValue = preorder[preStart];
    struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    root->val = rootValue;

    int rootIndex;
    for (rootIndex = inStart; rootIndex <= inEnd; rootIndex++) {
        if (inorder[rootIndex] == rootValue) {
            break;
        }
    }
    int leftTreeSize = rootIndex - inStart;
    root->left = buildTree(preorder, inorder, preStart + 1, preStart + leftTreeSize, inStart, rootIndex - 1);
    root->right = buildTree(preorder, inorder, preStart + leftTreeSize + 1, preEnd, rootIndex + 1, inEnd);
    return root;
}

int gethigh(struct TreeNode* root){
    if(root==NULL) return 0;
    int lefthigh=gethigh(root->left);
    int righthigh=gethigh(root->right);
    return (lefthigh>righthigh ? lefthigh:righthigh)+1;
}

int main() {
    int n;
    char preorder[100];
    char inorder[100];
    while (scanf("%d",&n) != EOF) {
        scanf("%s", preorder);
        scanf("%s", inorder);
        int preLen = strlen(preorder);
        int inLen = strlen(inorder);
        struct TreeNode* root = buildTree(preorder, inorder, 0, preLen - 1, 0, inLen - 1);
        printf("%d\n",gethigh(root));
    }
    return 0;
}